package com.tsmti.tags.grid;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.util.MapUtils;
import com.tsmti.core.util.MessageUtils;
import com.tsmti.core.util.ObjectUtils;
import com.tsmti.core.util.StringUtils;
import com.tsmti.system.entity.DataDictionary;
import com.tsmti.util.DictionaryUtils;
import com.tsmti.util.RedisGridFormatUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @Title: DataGridColumnTag.java
 * @Package
 * @Description: 数据列
 * @author:
 * @date: 2017年3月4日 下午9:03:39
 * @version
 * @Copyright: 2017
 *
 */
@SuppressWarnings("serial")
public class DataGridColumnTag extends AbstractGridHtmlTag {
	private final static String[] KEYS = { "prefix", "suffix", "formatterValue", "formatterClass", "formatterColor",
			"imageWidth", "imageHeight", "dict", "dateformat","redis" };

	/** 标题文字 */
	private String label;
	/** 列字段名称 */
	private String name;
	/** 默认为input，有dict时候，默认为select */
	private String queryMode = "input";
	/**查询条件默认值*/
	private Object queryValue;
	/**查询框事件*/
	private String queryEvent;
	/** 宽度 */
	private int width = 120;
	/** 是否为查询 */
	private Boolean query = Boolean.FALSE;
	/** 查询方式 */
	private String condition = "eq";
	/** 对齐方式 */
	private String align = "left";
	/** 是否开启排序 */
	private Boolean sortable = Boolean.TRUE;
	/** 是否为复选框 */
	private Boolean checkbox = Boolean.TRUE;
	/** 是否隐藏列 */
	private Boolean hidden = Boolean.FALSE;
	/** 字典类型 */
	private String dict;
	/** redis类型 */
	private String redis;
	/** 格式化，默认格式化有integer,currency,email,link,label,image,button,date */
	private String formatter;
	/** 格式化参数 */
	private String formatoptions;
	/** 前綴 */
	private String prefix;
	/** 后缀 */
	private String suffix;
	/** 格式化的值 */
	private String formatterValue;
	/** 格式化的样式 */
	private String formatterClass;
	/** 格式化的颜色 */
	private String formatterColor;
	/** 图片的宽度 */
	private String imageWidth;
	/** 图片的高度 */
	private String imageHeight;
	/** 时间格式化 */
	private String dateformat;
	/** 扩展设置 */
	private String columnSetting = "{}";
	/** 配置方法,为js方法，返回配置 */
	private String columnSettingCallback = "";
	/** 是否开启编辑 */
	private Boolean editable = Boolean.FALSE;
	/**
	 * 类型可以编辑的类型。可选值：text, textarea,select,password,date,autocomplete
	 * 不支持checkbox, button, image and file.
	 */
	private String edittype = "text";
	private String editdateformat = "yyyy-mm-dd";
	/** 行内编辑回调 */
	private String editoptions = "";
	/** 验证 */
	private String editrules;
	/** 验证规则 */
	private String datatype;
	/** 空验证 */
	private String nullmsg;
	/** 验证错误 */
	private String errormsg;
	/** 单元格式化*/
	private String cellattr;
	/**是否KEY*/
	private Boolean key;
	/**索引字段*/
	private String index;

	private Map<String, Object> queryStaticAttributes;
	private Map<String, Object> queryDynamicAttributes;
	private DataGridTag parentTag = null;

	@Override
	public int doEndTag() throws JspTagException {
		parentTag = (DataGridTag) findAncestorWithClass(this, DataGridTag.class);
		this.label = MessageUtils.getMessageOrSelf(label);
		addQuery();
		try {
			addColumn();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private void addQuery() {
		// 初始化数据
		if (query) {
			Map<String, Object> queryMap = new HashMap<String, Object>(16);
			queryMap.put("name", name);
			queryMap.put("label", label);
			queryMap.put("dateformat", dateformat);
			if (!StringUtils.isEmpty(dict)) {
				queryMap.put("dict", dict);
			}
			if (!StringUtils.isEmpty(redis)) {
				queryMap.put("redis", redis);
			}
			queryMap.put("queryMode", queryMode);
			queryMap.put("queryValue",queryValue);
			queryMap.put("condition", condition);
			queryMap.put("queryEvent",queryEvent);
			if (queryDynamicAttributes != null) {
				queryMap.putAll(queryDynamicAttributes);
			}
			if (queryStaticAttributes != null) {
				queryMap.putAll(queryStaticAttributes);
			}
			if (!queryMap.containsKey(Constants.CLASS_STRING)) {
			}
			if (Constants.QUERYMODE_RADIO.equals(queryMode) || Constants.QUERYMODE_CHECKBOX.equals(queryMode)) {
				queryMap.put("class", queryMap.get("class") + " i-checks");
			}
			parentTag.addQuery(queryMap);
		}
	}

	private void addColumn() throws Exception {
		Map<String, Object> columnMap = new HashMap<String, Object>(16);
		columnMap.putAll(staticAttributes);
		if (!StringUtils.isEmpty(editoptions)) {
			columnMap.put("editoptions", editoptions);
		}

		// 编辑的时候，行不需要格式化显示
		if (editable) {
			if (Constants.EDITTYPE_SELECT.equals(edittype)) {
				if (columnMap.get(editoptions) == null) {
					if (StringUtils.isEmpty(editoptions)) {
						if (!StringUtils.isEmpty(dict)) {
							columnMap.put("editoptions", dictToFormatterValue(dict));
						}
						if (!StringUtils.isEmpty(redis)) {
							columnMap.put("editoptions", redisToFormatterValue(redis));
						}
					}
				}else{
					columnMap.put("editoptions", editoptions);
				}
			}
			if (Constants.EDITTYPE_DATE.equals(edittype)) {
				if (StringUtils.isEmpty(editoptions)) {
					columnMap.put("editoptions", "date");
				}
				columnMap.put("edittype", "text");
			}
		}
		if (!StringUtils.isEmpty(dict)) {
			parentTag.putColumnDict(dict);
		}
		if (!StringUtils.isEmpty(redis)) {
			parentTag.putColumnRedis(redis,queryValue == null?null:String.valueOf(queryValue));
		}
		if (StringUtils.isEmpty(formatter)) {
			if (MapUtils.containsOrKeys(columnMap, KEYS)) {
				this.formatter = "label";
				columnMap.put("formatter", this.formatter);
			}
		}
		if (!StringUtils.isEmpty(this.dict) ) {
			this.formatterValue = dictToFormatterValue(dict);
			columnMap.put("formatterValue", this.formatterValue);
		}
		if (!StringUtils.isEmpty(this.redis) ) {
			this.formatterValue = redisToFormatterValue(redis);
			columnMap.put("formatterValue", this.formatterValue);
		}
		parentTag.addColumn(columnMap);
	}

	public String dictToFormatterValue(String dict) {
		String dictStr = "";
		List<DataDictionary> dictList = DictionaryUtils.getDictList(dict);
		for (DataDictionary dictEntity : dictList) {
			if (!StringUtils.isEmpty(dictStr)) {
				dictStr += ";";
			}
			dictStr += dictEntity.getCode() + ":" + dictEntity.getName();
		}
		return dictStr;
	}

	public String redisToFormatterValue(String redis) throws Exception {
		String redisStr = RedisGridFormatUtils.getGridFormatValues(redis);
		return redisStr;
	}

	@Override
	public void setStaticAttribute(String localName, Object value) throws JspException {
		if (this.staticAttributes == null) {
			this.staticAttributes = new HashMap<String, Object>(16);
		}
		if (this.queryStaticAttributes == null) {
			this.queryStaticAttributes = new HashMap<String, Object>(16);
		}
		if (!ObjectUtils.isNullOrEmpty(value)) {
			if (Constants.LOCALNAME_QUERY.equals(localName)) {
				return;
			} else if (Constants.LOCALNAME_CONDITION.equals(localName) || localName.startsWith(Constants.LOCALNAME_QUERY_PREFIX)) {
				// 去掉前缀
				localName = localName.replace(Constants.LOCALNAME_QUERY_PREFIX, localName);
				queryStaticAttributes.put(localName, value);
			} else {
				staticAttributes.put(localName, value);
			}
		}
		if (Constants.LOCALNAME_LABEL.equals(localName)) {
			staticAttributes.put(localName, MessageUtils.getMessageOrSelf((String) value));
		}
	}

	@Override
	public void setDynamicAttribute(String url, String localName, Object value) throws JspException {
		if (this.dynamicAttributes == null) {
			this.dynamicAttributes = new HashMap<String, Object>(16);
		}
		if (this.queryDynamicAttributes == null) {
			this.queryDynamicAttributes = new HashMap<String, Object>(16);
		}

		if (Constants.LOCALNAME_CONDITION.equals(localName) || localName.startsWith(Constants.LOCALNAME_QUERY_PREFIX)) {
			//去掉前缀
			localName = localName.replace(Constants.LOCALNAME_QUERY_PREFIX, "");
			queryDynamicAttributes.put(localName, value);
		} else {
			dynamicAttributes.put(localName, value);
		}
		if (Constants.LOCALNAME_LABEL.equals(localName)) {
			dynamicAttributes.put(localName, MessageUtils.getMessageOrSelf((String) value));
		}

	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public Boolean getQuery() {
		return query;
	}

	public void setQuery(Boolean query) {
		this.query = query;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public Boolean getSortable() {
		return sortable;
	}

	public void setSortable(Boolean sortable) {
		this.sortable = sortable;
	}

	public Boolean getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(Boolean checkbox) {
		this.checkbox = checkbox;
	}

	public Boolean getHidden() {
		return hidden;
	}

	public void setHidden(Boolean hidden) {
		this.hidden = hidden;
	}

	public String getDict() {
		return dict;
	}

	public void setDict(String dict) {
		this.dict = dict;
	}

	public String getQueryMode() {
		return queryMode;
	}

	public void setQueryMode(String queryMode) {
		this.queryMode = queryMode;
	}

	public Object getQueryValue() {
		return queryValue;
	}

	public void setQueryValue(Object queryValue) {
		this.queryValue = queryValue;
	}

	public String getQueryEvent() {
		return queryEvent;
	}

	public void setQueryEvent(String queryEvent) {
		this.queryEvent = queryEvent;
	}

	public String getDateformat() {
		return dateformat;
	}

	public void setDateformat(String dateformat) {
		this.dateformat = dateformat;
	}

	public String getColumnSetting() {
		return columnSetting;
	}

	public void setColumnSetting(String columnSetting) {
		this.columnSetting = columnSetting;
	}

	public String getColumnSettingCallback() {
		return columnSettingCallback;
	}

	public void setColumnSettingCallback(String columnSettingCallback) {
		this.columnSettingCallback = columnSettingCallback;
	}

	public String getFormatter() {
		return formatter;
	}

	public void setFormatter(String formatter) {
		this.formatter = formatter;
	}

	public String getFormatoptions() {
		return formatoptions;
	}

	public void setFormatoptions(String formatoptions) {
		this.formatoptions = formatoptions;
	}

	public Boolean getEditable() {
		return editable;
	}

	public void setEditable(Boolean editable) {
		this.editable = editable;
	}

	public String getEdittype() {
		return edittype;
	}

	public void setEdittype(String edittype) {
		this.edittype = edittype;
	}

	public String getEditoptions() {
		return editoptions;
	}

	public void setEditoptions(String editoptions) {
		this.editoptions = editoptions;
	}

	public String getEditdateformat() {
		return editdateformat;
	}

	public void setEditdateformat(String editdateformat) {
		this.editdateformat = editdateformat;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getFormatterValue() {
		return formatterValue;
	}

	public void setFormatterValue(String formatterValue) {
		this.formatterValue = formatterValue;
	}

	public String getFormatterClass() {
		return formatterClass;
	}

	public void setFormatterClass(String formatterClass) {
		this.formatterClass = formatterClass;
	}

	public String getFormatterColor() {
		return formatterColor;
	}

	public void setFormatterColor(String formatterColor) {
		this.formatterColor = formatterColor;
	}

	public String getImageWidth() {
		return imageWidth;
	}

	public void setImageWidth(String imageWidth) {
		this.imageWidth = imageWidth;
	}

	public String getImageHeight() {
		return imageHeight;
	}

	public void setImageHeight(String imageHeight) {
		this.imageHeight = imageHeight;
	}

	public String getDatatype() {
		return datatype;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	public String getNullmsg() {
		return nullmsg;
	}

	public void setNullmsg(String nullmsg) {
		this.nullmsg = nullmsg;
	}

	public String getErrormsg() {
		return errormsg;
	}

	public void setErrormsg(String errormsg) {
		this.errormsg = errormsg;
	}

	public String getEditrules() {
		return editrules;
	}

	public void setEditrules(String editrules) {
		this.editrules = editrules;
	}

	public String getCellattr() {
		return cellattr;
	}

	public void setCellattr(String cellattr) {
		this.cellattr = cellattr;
	}

	public String getRedis() {
		return redis;
	}

	public void setRedis(String redis) {
		this.redis = redis;
	}

	public Boolean getKey() {
		return key;
	}

	public void setKey(Boolean key) {
		this.key = key;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}
}